import { TimeStamp } from ".";

export interface LookupListItem {
    _id?: string;
    from: string;
    localField: string;
    as?: string; // 默认和localField相同
    foreignField?: string; // 默认为_id
}

export interface ParentFind {
    parentModelName: string; // 父表模型名,如DictionaryModel
    localField: string; // 父表关联字段名,如dictItems
}

// 代码生成主表
export interface CodeGenerate extends TimeStamp {
    _id: string;
    modelName: string;
    pageName?: string;
    collectionName?: string;
    desc?: string;
    fields: string[] | CreateCodeGenerateFieldType[]; // 与CodeGenerateField[]关联
    operations: ("create" | "getPageList" | "update" | "delete")[];
    resultTypes: ("admin" | "client")[];
    // 根据父_id查询子数据列表的配置，例子：getDictionaryItems方法
    parentFind?: ParentFind;
    // 关联查询配置
    lookupList?: LookupListItem[];
    permissionKey?: string;
}

// 代码生成字段子表
export interface CodeGenerateField extends TimeStamp {
    _id: string;
    name: string;
    type: "Number" | "String" | "Boolean" | "ObjectId" | "ObjectIdArray";
    isTable: boolean; // table是否显示该字段、是否查询该字段
    required: boolean;
    unique: boolean;
    queryType?: "fuzzy" | "precise"; // 查询方式：模糊查询、精确查询，字段不存在时不根据该字段查询
    sortType?: "asc" | "desc";
    dictionary?: string;
}

export type CreateCodeGenerateReq = Omit<CodeGenerate, "_id" | keyof TimeStamp> & {
    fields: Omit<CodeGenerateField, "_id" | keyof TimeStamp>[];
};

export interface UpdateCodeGenerateReq extends Omit<CodeGenerate, "fields" | "createdAt" | "updatedAt"> {
    fields: Omit<CodeGenerateField, "_id" | keyof TimeStamp>[];
}

export interface CreateCodeGenerateFieldType {
    _id: string;
    name: string;
    type: "Number" | "String" | "Boolean" | "ObjectId" | "ObjectIdArray";
    isTable: boolean | string;
    isForm: boolean | string;
    required: boolean | string;
    unique: boolean | string;
    queryType?: "fuzzy" | "precise";
    sortType?: "asc" | "desc";
    dictionary?: string;
}
